/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao;

import org.apache.commons.collections.list.AbstractListDecorator;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * List for result set with details of total result size and paging details.
 * @author altuure
 */
public class SearchResultList<T> extends AbstractListDecorator implements Serializable {
    private static final long serialVersionUID = 1L;
    /** Paging details. */
    private YPage paging;
    /** total result size. */
    private long totalCount;

    /** default constructor. */
    public SearchResultList() {
        super(Collections.emptyList());
        totalCount = 0;
    }

    /**
     * list delegator.
     * @param result delegated list
     */
    public SearchResultList(List<T> result) {
        this(result, result.size(), null);

    }

    /**
     * no paged list delegator.
     * @param result
     * @param paging
     */
    public SearchResultList(List<T> result, YPage paging) {
        this(result, result.size(), paging);
    }

    /**
     * no paged list delegator.
     * @param result
     * @param limit
     */
    public SearchResultList(List<T> result, YLimit limit) {
        this(result, result.size(), YPage.copy(limit));
    }

    /**
     * paged list delegator.
     * @param result list of items
     * @param totalCount result set size
     * @param paging page details
     */
    public SearchResultList(List<T> result, long totalCount, YPage paging) {
        super(result);
        this.totalCount = totalCount;
        this.paging = paging;
    }

    /** paging details. */
    public YPage getPaging() {
        return paging;
    }

    /** size of result set.
     * @return total item count*/
    public long totalCount() {
        return totalCount;
    }

    /**
     *  page index
     * @return     page index
     */
    public int getPageIndex(){
        return (int)paging.getFirstResult()/paging.getPageSize();
    }

    /**
     *
     * @return page count
     */
    public int getPageCount(){
        int i = (int) totalCount / paging.getPageSize();
        if(totalCount%paging.getPageSize()!=0)
            i=i+1;
        return i;
    }
    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.commons.collections.collection.AbstractCollectionDecorator
     * #toString()
     */
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("SearchResultList [paging=");
        builder.append(paging);
        builder.append(", totalCount=");
        builder.append(totalCount);
        builder.append("]");
        return builder.toString();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.commons.collections.collection.AbstractCollectionDecorator
     * #hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((paging == null) ? 0 : paging.hashCode());
        result = prime * result + (int) (totalCount ^ (totalCount >>> 32));
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.commons.collections.collection.AbstractCollectionDecorator
     * #equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (!(obj instanceof SearchResultList))
            return false;
        @SuppressWarnings("unchecked")
        SearchResultList<T> other = (SearchResultList<T>) obj;
        if (paging == null) {
            if (other.paging != null)
                return false;
        } else if (!paging.equals(other.paging))
            return false;
        if (totalCount != other.totalCount)
            return false;
        return true;
    }
}
